﻿Imports System.Data
Imports System.ComponentModel
Imports CRM.BusinessLayer
Partial Public Class frmAdminCreateLetterPack

    Private Sub Btn_Cancel_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_Cancel.Click
        Me.Close()
    End Sub

    Private Sub Btn_Save_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_Save.Click
        Try

            Dim StrSQL As String, StrMess As String, Rst As DataTable, LPID As Long

            If Me.Txt_Name.Text.Trim = String.Empty Then
                Call MsgBox("Please enter a valid name", , "Error")
                Exit Sub
            End If

            If Me.Combo_Type.SelectedIndex = -1 Then
                Call MsgBox("Please select a valid Type", , "Error")
                Exit Sub
            End If

            StrSQL = "Select Letterpack_ID from D_Letterpacks"
            StrSQL = StrSQL & " WHERE Letterpack_Name = '" & Me.Txt_Name.Text.Trim & "'"

            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

            If Rst.Rows.Count > 0 Then
                StrMess = "Letterpack number " & Rst.Rows(0)("Letterpack_ID") & " already has that name, to avoid confusion please choose another"
                Rst.Dispose()
                Call MsgBox(StrMess, , "Error")
                Exit Sub

            Else
                Rst.Dispose()
            End If

            StrSQL = "Insert into D_Letterpacks "
            StrSQL = StrSQL & "(Letterpack_Name, Letterpack_LetterTypeID)"
            StrSQL = StrSQL & "VALUES('" & Me.Txt_Name.Text.Trim & "'," & Me.Combo_Type.SelectedValue & ")"

            If CommonDataFunctions.ExecuteSQL(StrSQL) = False Then Exit Sub

            StrSQL = "Select Letterpack_ID from D_Letterpacks"
            StrSQL = StrSQL & " WHERE Letterpack_Name = '" & Me.Txt_Name.Text.Trim & "'"
            StrSQL = StrSQL & " Order by Letterpack_ID desc"

            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

            If Rst.Rows.Count = 0 Then
                'Error
                Rst.Dispose()
                Call MsgBox("New Letterpack not found", , "Error")
                Exit Sub
            End If

            'Rst.MoveFirst()

            LPID = Rst.Rows(0)("Letterpack_id")

            Rst.Dispose()

            StrMess = "New Letterpack created of type " & Me.Combo_Type.Text & ""
            StrMess = StrMess & vbCrLf & vbCrLf
            StrMess = StrMess & "ID     : " & LPID & vbCrLf
            StrMess = StrMess & "Name   : " & Me.Txt_Name.Text

            Call (New TasksEvents).GblFunc_Add_SysEvent("Admin (SL)", StrMess)
            Me.Close()
            ' DoCmd.Close(acForm, "FMA_CreateLetterPack")

            'Form_FMA_LetterPacks.List_Letterpacks.Requery()
            'Form_FMA_LetterPacks.List_Letterpacks = LPID

            ' Call Form_FMA_LetterPacks.List_Letterpacks_DblClick(0)

            

        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminCreateLetterPack -Btn_Save_Click")
        End Try
    End Sub

    Private Sub frmAdminCreateLetterPack_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        Dim strSQL As String = "select LetterType_ID,LetterType_Name from i_SL_Lettertypes"
        Dim letterTypes As DataTable = CommonDataFunctions.GetSQLDatatable(strSQL)
        With Me.Combo_Type
            .SelectedValuePath = "LetterType_ID"
            .DisplayMemberPath = "LetterType_Name"
            .ItemsSource = CType(letterTypes, IListSource).GetList
            .SelectedIndex = -1
        End With
    End Sub
End Class
